package com.woniuxy.admin.service.impl;

import com.woniuxy.admin.exception.PawnShopException;
import com.woniuxy.admin.exception.PawnWarehouseException;
import com.woniuxy.admin.entity.PawnShop;
import com.woniuxy.admin.entity.PawnShopWarehouse;
import com.woniuxy.admin.entity.PawnWarehouse;
import com.woniuxy.admin.mapper.PawnShopMapper;
import com.woniuxy.admin.mapper.PawnShopWarehouseMapper;
import com.woniuxy.admin.mapper.PawnWarehouseMapper;
import com.woniuxy.admin.service.PawnShopWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class PawnShopWarehouseServiceImpl extends ServiceImpl<PawnShopWarehouseMapper, PawnShopWarehouse> implements PawnShopWarehouseService {

    @Resource
    private PawnShopMapper pawnShopMapper;
    @Resource
    private PawnWarehouseMapper pawnWarehouseMapper;


    /**
     * 绑定门店和仓库
     * @param shopId 门店id
     * @param warehouseId 仓库id
     */
    @Override
    public void bindingShopWarehouse(Integer shopId, Integer warehouseId) {
        PawnShop pawnShop = pawnShopMapper.selectById(shopId);
        if(pawnShop==null) throw new PawnShopException(30002,"门店不存在");
        PawnWarehouse pawnWarehouse = pawnWarehouseMapper.selectById(warehouseId);
        if(pawnWarehouse==null) throw  new PawnWarehouseException(40002,"仓库不存在");
        if(pawnWarehouse.getState()!=0) throw new PawnShopException(10002,"仓库已被禁用");
        List<PawnShopWarehouse> list = this.list();
        if(list!=null&&list.size()>0){
            for (PawnShopWarehouse pawnShopWarehouse : list) {
                if(pawnShopWarehouse.getShopId()==shopId){
                    if(pawnShopWarehouse.getWarehouseId() == warehouseId){
                        throw new PawnShopException(50001,"该门店已经绑定该仓库");
                    }
                }
            }
        }
        PawnShopWarehouse pawnShopWarehouse = new PawnShopWarehouse();
        pawnShopWarehouse.setShopId(shopId);
        pawnShopWarehouse.setWarehouseId(warehouseId);
        this.save(pawnShopWarehouse);
    }

    /**
     * 取消门店和仓库绑定
     * @param shopId 门店id
     * @param warehouseId 仓库id
     */
    @Override
    public void cancelBindingShopWarehouse(Integer shopId, Integer warehouseId) {
        PawnShop pawnShop = pawnShopMapper.selectById(shopId);
        if(pawnShop==null) throw new PawnShopException(30002,"门店不存在");
        PawnWarehouse pawnWarehouse = pawnWarehouseMapper.selectById(warehouseId);
        if(pawnWarehouse==null) throw  new PawnWarehouseException(40002,"仓库不存在");
        List<PawnShopWarehouse> list = this.list();
        if(list!=null&&list.size()>0){
            for (PawnShopWarehouse pawnShopWarehouse : list) {
                if(pawnShopWarehouse.getShopId()==shopId){
                    if(pawnShopWarehouse.getWarehouseId() == warehouseId){
                        this.removeById(pawnShopWarehouse);
                        return;
                    }
                }
            }
        }
    }
}
